package com.otod.sys.file.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.constants.CommonConstant;
import com.otod.commons.utils.Collections3;
import com.otod.commons.utils.DateUtils;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.sys.file.service.IFileService;
import com.otod.sys.vo.FileVo;

/**
 * 文件操作
 * @author jqiu
 */
@Controller
@RequestMapping("sys/file")
public class FileController extends BaseController{
	@Autowired
	private IFileService fileService;
	
	
	@RequestMapping(value = "findByBusiId", method = RequestMethod.GET)
	@ResponseBody
	public List<FileVo> findByBusiId(String busiId,HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<FileVo> list = fileService.queryFileByBusiId(busiId);
		if(Collections3.isEmpty(list)){
			return new ArrayList<FileVo>();
		}
		for (FileVo fileVo : list) {
			fileVo.setDateFormat(DateUtils.formatDate(fileVo.getCreateDt()));
		}
		return list;
	}
	
	@RequestMapping(value = "deleteById", method = RequestMethod.GET)
	@ResponseBody
	public String deleteById(String id,HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("uuid", id);
		map.put("isDel", CommonConstant.COMMON_YES);
		super.setUpdateOperInfo(map);
		fileService.update(map);
		return JsonSuccess.factory("删除成功");
	}
	
	@RequestMapping(value = "submitAuditOperation", method = RequestMethod.GET)
	@ResponseBody
	public String submitAuditOperation(String fileId,String status) throws AppException {
		if(StringUtils.isEmpty(fileId)||StringUtils.isEmpty(status)){
			throw new AppException("参数异常！");
		}
		FileVo fileVo = fileService.getById(fileId);
		if(fileVo == null){
			throw new AppException("文件不存在，或已被删除");
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("uuid", fileId);
		map.put("auditStatus", status);
		super.setUpdateOperInfo(map);
		
		fileService.update(map);
		
		return JsonSuccess.factory("操作成功");
	}
	
}
